<?php

namespace app\merchant\model;

use app\common\lib\ValidateExtend;
use think\Loader;
use think\Model;
use think\Validate;

class BorrowRequest extends Model
{

    //设置当前模型对应的完整数据表名称
    protected $table = 'tp_borrow_request';

    const TYPE_PERSON_SHOPPING =2 ;//消费贷
    const TYPE_PERSON_CAR = 1 ;//车贷
    const TYPE_COMPANY =3 ;//企业贷

    const STATUS_DEFAULT = 0;//待审
    const STATUS_YES = 1;//通过
    const STATUS_NO = 2;//拒绝
    const STATUS_TARGET = 3;//已打包

    //1：到期还本息；2：到期还本 按月付息，；3：到期还本 按季付息；4：等额本息
    const REPAYMENT_TYPE_DQHBX = 1;
    const REPAYMENT_TYPE_DQHB_YFX = 2;
    const REPAYMENT_TYPE_DQHB_JFX = 3;
    const REPAYMENT_TYPE_DEBX = 4;


    //还款类型
    public static function repaymentTypeMap(){
        return [
            self::REPAYMENT_TYPE_DQHBX=>'到期还本息',
            self::REPAYMENT_TYPE_DQHB_YFX=>'到期还本,按月付息',
            self::REPAYMENT_TYPE_DQHB_JFX=>'到期还本,按季付息',
            self::REPAYMENT_TYPE_DEBX=>'等额本息',
        ];
    }

    //还款类型
    public static function statusMap(){
        return [
            self::STATUS_DEFAULT=>'待审核',
            self::STATUS_YES=>'审核通过',
            self::STATUS_NO=>'审核未通过',
            self::STATUS_TARGET=>'已打包',
        ];
    }

    /*excle 扩展字段与sql中json的key的map
     * key :excel的key
     * value titile：名称，field：sql的key
     *  json. group_id 该字段所属的分组信息
     * */
    public static function getExcelFiledMap($int_type){
        $map = [];
        switch ($int_type){
            case self::TYPE_PERSON_SHOPPING:
                $sql_map = [
                    'money' => ['title'=>'借款金额','field' => 'money','field_tab'=>1],
                    'duration' => ['title'=>'借款期限','field' => 'duration','field_tab'=>1],
                    'repay_type' => ['title'=>'还款方式','field' => 'repayment_type','field_tab'=>1],
                    'repay_source' => ['title'=>'还款来源','field' => 'repayment_source','field_tab'=>1],
                    'money_use_for' => ['title'=>'资金用途','field' => 'money_use_for','field_tab'=>1],
                ];
                $json_map =[
                    'mobile' => ['title'=>'手机号','field' => 'phone','field_tab'=>1, 'group_id'=>1],
                    'name' => ['title'=>'真实姓名','field' => 'real_name','field_tab'=>1, 'group_id'=>1],
                    'idcard' => ['title'=>'身份证','field' => 'idcard','field_tab'=>1, 'group_id'=>1],
                    'location' => ['title'=>'所在省市','field' => 'location','field_tab'=>2, 'group_id'=>1],
                    'address' => ['title'=>'居住地址','field' => 'address','field_tab'=>2, 'group_id'=>1],
                    'contacts' => ['title'=>'常用联系人','field' => 'contacts','field_tab'=>2, 'group_id'=>1],
                    'company_name' => ['title'=>'所在公司名称','field' => 'company_name','field_tab'=>2, 'group_id'=>1],
                    'industry' => ['title'=>'所在行业类别','field' => 'industry','field_tab'=>2, 'group_id'=>1],
                    'income' => ['title'=>'收入情况','field' => 'income','field_tab'=>2,'group_id' =>2],
                    'loan' => ['title'=>'负债情况','field' => 'loan','field_tab'=>2,'group_id' =>2],
                    'jxzk' => ['title'=>'借款人经营状况','field' => 'jxzk','field_tab'=>2, 'group_id' =>2,],
                    'cwqk' => ['title'=>'借款人财务状况','field' => 'cwqk','field_tab'=>2, 'group_id' =>2],
                    'ssql' => ['title'=>'借款人涉诉情况','field' => 'ssql','field_tab'=>2, 'group_id' =>2],
                    'xzcf' => ['title'=>'借款人行政处罚','field' => 'xzcf','field_tab'=>2, 'group_id' =>2],
                    'zjyy' => ['title'=>'借款资金运用情况','field' => 'zjyy','field_tab'=>2, 'group_id' =>2],
                    'file_srzm' => ['title'=>'收入证明','field' => 'file_srzm',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>2, 'group_id' =>3],
                    'file_zx' => ['title'=>'征信证明','field' => 'file_zx',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>2, 'group_id' =>3],
                ];
                $arr_fieldTab=[
                    '1'=>['title'=>'基本信息'],
                    '2'=>['title'=>'其他信息'],
                ];
                //json 字段所属分组，所属类型 type： 1.文本 2.图片
                $arr_json_filed_group = [
                    1 => [
                        'type' =>1,
                        'group_id' =>1,
                        'title' =>'基本信息'
                    ],
                    2 => [
                        'type' => 1,
                        'group_id' =>2,
                        'title' => '其他信息'
                    ],
                    3 => [
                        'type' => 3,
                        'group_id' =>3,
                        'title' => '证明信息'
                    ],
                ];
                break;
            case self::TYPE_PERSON_CAR:
                $sql_map = [
                    'money' => ['title'=>'借款金额','field' => 'money','field_tab'=>2],
                    'duration' => ['title'=>'借款期限','field' => 'duration','field_tab'=>2],
                    'repay_type' => ['title'=>'还款方式','field' => 'repayment_type','field_tab'=>2],
                    'repay_source' => ['title'=>'还款来源','field' => 'repayment_source','field_tab'=>2],
                    'money_use_for' => ['title'=>'资金用途','field' => 'money_use_for','field_tab'=>2],
                ];
                $json_map =[
                    'mobile' => ['title'=>'手机号','field' => 'phone','field_tab'=>1, 'group_id' =>1],
                    'name' => ['title'=>'真实姓名','field' => 'real_name','field_tab'=>1, 'group_id' =>1],
                    'idcard' => ['title'=>'身份证','field' => 'idcard','field_tab'=>1, 'group_id' =>1],
                    'location' => ['title'=>'所在省市','field' => 'location','field_tab'=>3, 'group_id' =>3],
                    'address' => ['title'=>'居住地址','field' => 'address','field_tab'=>3, 'group_id' =>3],
                    'contacts' => ['title'=>'常用联系人','field' => 'contacts','field_tab'=>3, 'group_id' =>3],
                    'company_name' => ['title'=>'所在公司名称','field' => 'company_name','field_tab'=>3, 'group_id' =>3],
                    'industry' => ['title'=>'所在行业类别','field' => 'industry','field_tab'=>3, 'group_id' =>3],
                    'income' => ['title'=>'收入情况','field' => 'income','field_tab'=>3, 'group_id' =>3],
                    'loan' => ['title'=>'负债情况','field' => 'loan','field_tab'=>3, 'group_id' =>3],
                    'jxzk' => ['title'=>'借款人经营状况','field' => 'jxzk','field_tab'=>3, 'group_id' =>3],
                    'cwqk' => ['title'=>'借款人财务状况','field' => 'cwqk','field_tab'=>3, 'group_id' =>3],
                    'ssql' => ['title'=>'借款人涉诉情况','field' => 'ssql','field_tab'=>3, 'group_id' =>3],
                    'xzcf' => ['title'=>'借款人行政处罚','field' => 'xzcf','field_tab'=>3, 'group_id' =>3],
                    'zjyy' => ['title'=>'借款资金运用情况','field' => 'zjyy','field_tab'=>3, 'group_id' =>3],
                    'car_num' => ['title'=>'车牌号','field' => 'car_num','field_tab'=>2, 'group_id' =>2],
                    'car_type' => ['title'=>'车辆型号','field' => 'car_type','field_tab'=>2, 'group_id' =>2],
                    'car_price' => ['title'=>'评估金额','field' => 'car_price','field_tab'=>2, 'group_id' =>2],
                    'percent' => ['title'=>'放款比率','field' => 'percent','field_tab'=>2, 'group_id' =>2],
                    'ceb_fkje' => ['title'=>'超额宝放款金额','field' => 'ceb_fkje','field_tab'=>2, 'group_id' =>2],
                    'file_srzm' => ['title'=>'收入证明','field' => 'file_srzm',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>3, 'group_id' =>4],
                    'file_zx' => ['title'=>'征信证明','field' => 'file_zx',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>3, 'group_id' =>4],
                    'file_jkxy' => ['title'=>'借款协议扫描件','field' => 'file_jkxy',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>3 , 'group_id' =>4],
                    'file_zrxy' => ['title'=>'转让协议','field' => 'file_zrxy',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>3, 'group_id' =>4],
                    'file_zyht' => ['title'=>'抵押合同','field' => 'file_zyht',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>3, 'group_id' =>4],
                    'file_carPhoto' => ['title'=>'车辆照片','field' => 'file_carPhoto',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>3, 'group_id' =>4],
                ];
                $arr_fieldTab=[
                    '1'=>['title'=>'基本信息'],
                    '2'=>['title'=>'借款信息'],
                    '3'=>['title'=>'其他信息'],
                ];

                $arr_json_filed_group = [
                    1 => [
                        'type' =>1,
                        'group_id' =>1,
                        'title' =>'基本信息'
                    ],
                    2 => [
                        'type' => 1,
                        'group_id' =>2,
                        'title' => '借款信息'
                    ],
                    3 => [
                        'type' => 1,
                        'group_id' =>3,
                        'title' => '借款信息'
                    ],
                    4 => [
                        'type' => 2,
                        'group_id' =>4,
                        'title' => '证明信息'
                    ],
                ];
                break;
            case self::TYPE_COMPANY:
                $sql_map = [
                    'money' => ['title'=>'借款金额','field' => 'money','field_tab'=>4],
                    'duration' => ['title'=>'借款期限','field' => 'duration','field_tab'=>4],
                    'repay_type' => ['title'=>'还款方式','field' => 'repayment_type','field_tab'=>4],
                    'repay_source' => ['title'=>'还款来源','field' => 'repayment_source','field_tab'=>4],
                    'money_use_for' => ['title'=>'资金用途','field' => 'money_use_for','field_tab'=>4],
                ];
                $json_map =[
                    'company_name' => ['title'=>'企业名称','field' => 'company_name','field_tab'=>1,'group_id' =>1 ],
                    'company_synopsis' => ['title'=>'企业简介','field' => 'company_synopsis','field_tab'=>1, 'group_id' =>1],
                    'business_scope' => ['title'=>'营业范围','field' => 'business_scope','field_tab'=>1, 'group_id' =>1],
                    'license_no' => ['title'=>'营业执照','field' => 'license_no','field_tab'=>1, 'group_id' =>1],
                    'license_expire_date' => ['title'=>'营业执照有效期','field' => 'license_expire_date','field_tab'=>1, 'group_id' =>1],
                    'license_address' => ['title'=>'营业执照所在地','field' => 'license_address','field_tab'=>1, 'group_id' =>1],
                    'organization_no' => ['title'=>'组织机构代码','field' => 'organization_no','field_tab'=>1, 'group_id' =>1],
                    'address' => ['title'=>'企业地址','field' => 'address','field_tab'=>1, 'group_id' =>1],
                    'industry' => ['title'=>'所在行业类别','field' => 'industry','field_tab'=>1, 'group_id' =>1],
                    'website' => ['title'=>'网站网址','field' => 'website','field_tab'=>1, 'group_id' =>1],
                    'telephone' => ['title'=>'联系电话','field' => 'telephone','field_tab'=>1, 'group_id' =>1],
                    'email' => ['title'=>'联系邮件','field' => 'email','field_tab'=>1, 'group_id' =>1],
                    'legal_person' => ['title'=>'企业法人','field' => 'legal_person','field_tab'=>2, 'group_id' =>2],
                    'mobile' => ['title'=>'法人手机号','field' => 'mobile','field_tab'=>2, 'group_id' =>2],
                    'idcard' => ['title'=>'法人身份证','field' => 'idcard','field_tab'=>2, 'group_id' =>2],
                    'bank_code' => ['title'=>'银行编号','field' => 'bank_code','field_tab'=>3, 'group_id' =>3],
                    'bank' => ['title'=>'开户行','field' => 'bank','field_tab'=>3, 'group_id' =>3],
                    'bank_account_no' => ['title'=>'开户行卡号','field' => 'bank_account_no','field_tab'=>3, 'group_id' =>3],
                    'bank_location' => ['title'=>'开户行省','field' => 'bank_location','field_tab'=>3, 'group_id' =>3],
                    'bank_city' => ['title'=>'开户行城市','field' => 'bank_city','field_tab'=>3, 'group_id' =>3],
                    'bank_branch' => ['title'=>'开户行支行','field' => 'bank_branch','field_tab'=>3, 'group_id' =>3],
                    'income' => ['title'=>'收入情况','field' => 'income','field_tab'=>5, 'group_id' =>5],
                    'loan' => ['title'=>'负债情况','field' => 'loan','field_tab'=>5, 'group_id' =>5],
                    'jxzk' => ['title'=>'借款人经营状况','field' => 'jxzk','field_tab'=>5, 'group_id' =>5],
                    'cwqk' => ['title'=>'借款人财务状况','field' => 'cwqk','field_tab'=>5, 'group_id' =>5],
                    'ssql' => ['title'=>'借款人涉诉情况','field' => 'ssql','field_tab'=>5, 'group_id' =>5],
                    'xzcf' => ['title'=>'借款人行政处罚','field' => 'xzcf','field_tab'=>5, 'group_id' =>5],
                    'zjyy' => ['title'=>'借款资金运用情况','field' => 'zjyy','field_tab'=>5, 'group_id' =>5],
                    'file_jkxy' => ['title'=>'借款协议扫描件','field' => 'file_jkxy','field_type'=>'file_url',
                        'ext'=>'jpg','field_tab'=>5, 'group_id' =>6],
                    'file_zrxy' => ['title'=>'转让协议','field' => 'file_zrxy',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>5, 'group_id' =>6],
                    'file_carPhoto' => ['title'=>'车辆照片','field' => 'file_carPhoto',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>5, 'group_id' =>6],
                    'license_file_src' => ['title'=>'营业执照文件地址','field' => 'license_file_src',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>5, 'group_id' =>6],
                    'org_no_src' => ['title'=>'组织机构证','field' => 'org_no_src',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>5, 'group_id' =>6],
                    'tax_no_src' => ['title'=>'税务登记证','field' => 'tax_no_src',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>5, 'group_id' =>6],
                    'open_account_no_src' => ['title'=>'开户许可证','field' => 'open_account_no_src',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>5, 'group_id' =>6],
                    'front_card_src' => ['title'=>'身份证正面','field' => 'front_card_src',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>2, 'group_id' =>6],
                    'back_card_src' => ['title'=>'身份证反面','field' => 'back_card_src',
                        'field_type'=>'file_url','ext'=>'jpg','field_tab'=>2, 'group_id' =>6],
                    ];
                $arr_fieldTab=[
                    '1'=>['title'=>'基本信息'],
                    '2'=>['title'=>'法人信息'],
                    '3'=>['title'=>'银行卡信息'],
                    '4'=>['title'=>'借款信息'],
                    '5'=>['title'=>'其他信息'],
                ];

                $arr_json_filed_group = [
                    1 => [
                        'type' =>1,
                        'group_id' =>1,
                        'title' =>'基本信息'
                    ],
                    2 => [
                        'type' => 1,
                        'group_id' =>2,
                        'title' => '法人信息'
                    ],
                    3 => [
                        'type' => 1,
                        'group_id' =>3,
                        'title' => '银行卡信息'
                    ],
                    4 => [
                        'type' => 1,
                        'group_id' =>4,
                        'title' => '借款信息'
                    ],
                    5 => [
                        'type' => 1,
                        'group_id' =>5,
                        'title' => '其他信息'
                    ],
                    6 => [
                        'type' => 2,
                        'group_id' =>6,
                        'title' => '其他信息'
                    ],
                ];

                break;
        }
        $map['sql_map'] = $sql_map;
        $map['json_map'] = $json_map;
        $map['field_tab'] = $arr_fieldTab;//字段栏目分类 前端页面tab分组
        $map['json_filed_group_map'] = $arr_json_filed_group;//字段栏目分类 前端页面tab分组
        return $map;
    }


    /*
     *@desc 生成批次号
     * */
    public static function generateBatchNum($int_uid){
        return date("YmdHis".$int_uid,time());
    }

    /*
     * excel key 验证excel字段是否合法
     * */
    public static function validateExcelData($int_type, $arr_data){
        $map = [];
        $arr_keyRepayTypes = self::repaymentTypeMap();
        $arr_repayTypes = array_keys($arr_keyRepayTypes);
        $str_repayTypes = '';
        foreach ($arr_keyRepayTypes as $key => $val){
            $str_repayTypes .= $key.':'.$val.';';
        }

        $arr_bankList = Config('bank_list');
        $arr_bankListCodes = array_keys($arr_bankList);
        $str_codesDesc = '';
        foreach ($arr_bankList as $key => $val){
            $str_codesDesc .= $key.':'.$val.';';
        }

        switch ($int_type){
            case self::TYPE_PERSON_SHOPPING:
                $arr_rule = [
                    'mobile'=>'require|phone',
                    'duration'=>'require|number',
                    'money'=>'require|number|egt:100',
                    'repay_type'=>'require|in:'.implode(',',$arr_repayTypes),
                ];
                $arr_msg = [
                    'mobile.phone' => '手机号不合法！',
                    'duration.require' => '还款期限必填',
                    'duration.number' => '还款期限必须为数字',
                    'money.require' => '借款金额必填',
                    'money.number' => '借款金额必须为数字',
                    'money.egt' => '借款金额最小100元',
                    'repay_type.require' => '还款类型必填',
                    'repay_type.in' => '还款类型必须为数字,'.$str_repayTypes,
                ];
                break;
            case self::TYPE_PERSON_CAR:
                $arr_rule = [
                    'mobile'=>'phone',
                    'duration'=>'require|number',
                    'money'=>'require|number|egt:100',
                    'repay_type'=>'require|in:'.implode(',',$arr_repayTypes),
                ];
                $arr_msg = [
                    'mobile.phone' => '手机号不合法！',
                    'duration.require' => '还款期限必填',
                    'duration.number' => '还款期限必须为数字',
                    'money.require' => '借款金额必填',
                    'money.number' => '借款金额必须为数字',
                    'money.egt' => '借款金额最小100元',
                    'repay_type.require' => '还款类型必填',
                    'repay_type.in' => '还款类型必须为数字,'.$str_repayTypes,
                ];
                break;
            case self::TYPE_COMPANY:
                $arr_rule = [
                    'mobile'=>'phone',
                    'company_name'=>'require',
                    'business_scope'=>'require',
                    'legal_person'=>'require',
                    'idcard'=>'require',
                    'duration'=>'require|number',
                    'money'=>'require|number|egt:100',
                    'email'=>'require|email',
                    'license_no'=>'require',
                    'repay_type'=>'require|in:'.implode(',',$arr_repayTypes),
                    'bank_code'=>'require|in:'.implode(',',$arr_bankListCodes),
                ];
                $arr_msg = [
                    'mobile.phone' => '手机号不合法！',
                    'legal_person.require' => '企业法人必填',
                    'company_name.require' => '企业名称必填',
                    'business_scope.require' => '营业范围必填',
                    'idcard.require' => '法人证件号必填',
                    'duration.require' => '还款期限必填',
                    'duration.number' => '还款期限必须为数字',
                    'money.require' => '借款金额必填',
                    'money.number' => '借款金额必须为数字',
                    'money.egt' => '借款金额最小100元',
                    'email.require' => '电子邮箱必填必填',
                    'email.email' => '请输入正确格式的电子邮箱',
                    'repay_type.require' => '还款类型必填',
                    'repay_type.in' => '还款类型必须为数字,'.$str_repayTypes,
                    'license_no.require' => '营业执照号必填',
                    'bank_code.require' => '银行编码必填',
                    'bank_code.in' => '银行编号不合法，'.$str_codesDesc,
                ];
                break;
        }
        $validate = new ValidateExtend($arr_rule, $arr_msg);
        $result   = $validate->batch()->check($arr_data);
        return $validate->getError();
    }

    /*
     * 注意该方法下的下 的field 的字段不可修改,可以新增
     * */
    public function getBorrowRequestDetail($int_borrowRequestId){
        $info = self::alias('bq')
            ->where(["bq.id"=> $int_borrowRequestId])
            ->join('tp_borrower user', 'user.uid = bq.borrow_uid','LEFT' )
            ->join('tp_user user_2', 'user_2.uid = bq.borrow_uid','LEFT' )
            ->join('tp_borrow_request_detail bq_detail', 'bq_detail.borrow_request_id = bq.id','LEFT' )
            ->field([
                'user_2.uid as uid',
                'user_2.real_name as real_name',
                'user_2.idcard as idcard',
                'user_2.third_uid as third_uid',
                'user_2.user_phone as user_phone',
                'user_2.is_set_pay_password as is_set_pay_password',
                'user.fornt_card as fornt_card',
                'user.back_card as back_card',
                'bq.id as bq_id',
                'bq.type as bq_type',
                'bq.batch_number as batch_number',
                'bq.duration as duration',
                'bq.money as money',
                'bq.repayment_type as repayment_type',
                'bq.repayment_source as repayment_source',
                'bq.status as status',
                'bq.money_use_for as money_use_for',
                'bq.create_time as create_time',
                'bq.update_time as update_time',
                'bq_detail.extra_filed as extra_filed',
            ])
            ->find();
        return $info;
    }

    /*
     * 获取企业贷 详情
    * 注意该放下的下 的field 的字段不可修改,可以新增
    * */
    public function getCompanyBorrowRequestDetail($int_borrowRequestId){
        $info = self::alias('bq')
            ->where(["bq.id"=> $int_borrowRequestId])
            ->join('tp_borrower user', 'user.uid = bq.borrow_uid','LEFT' )
            ->join('tp_user user_2', 'user_2.uid = bq.borrow_uid','LEFT' )
            ->join('tp_user_company user_company', 'user_company.user_id = bq.borrow_uid','LEFT' )
            ->join('tp_borrow_request_detail bq_detail', 'bq_detail.borrow_request_id = bq.id','LEFT' )
            ->field([
                'user_2.uid as uid',
                'user_company.legal_person as real_name',
                'user_company.cert_no as idcard',
                'user_2.third_uid as third_uid',
                'user_2.user_phone as user_phone',
                'user_2.is_set_pay_password as is_set_pay_password',
                'user.fornt_card as fornt_card',
                'user.back_card as back_card',
                'bq.id as bq_id',
                'bq.type as bq_type',
                'bq.batch_number as batch_number',
                'bq.duration as duration',
                'bq.money as money',
                'bq.repayment_type as repayment_type',
                'bq.repayment_source as repayment_source',
                'bq.status as status',
                'bq.money_use_for as money_use_for',
                'bq.create_time as create_time',
                'bq.update_time as update_time',
                'bq_detail.extra_filed as extra_filed',
            ])
            ->find();
        return $info;
    }

}
